#!/usr/bin/env python
#coding=utf-8


import argparse
import os, sys, time, re, shutil
import subprocess
from multiprocessing import cpu_count

# 获取调用脚本所在路径
# 即项目路径
project_path = os.path.abspath(sys.path[0])
# 输出路径
output_path  = project_path+'/build'
# sdk 路径
fs_sdk_path  = os.getenv('FS_PATH')

# 项目名称在项目所在目录的脚本指定
print('project_pafs_sdk_pathth={}'.format(fs_sdk_path))
print('project_path           ={}'.format(project_path))
print('output_path            ={}'.format(output_path))



#-------------------------------------------------------------------------------
# 解析输入脚本参数
# description: 帮助文档前显示的文本
parser = argparse.ArgumentParser(description='project cmd , e.g. `python project.py build`')

# (可选) 生成配置文件
parser.add_argument('--config_file',
                    help='out config file name,default `project.config`',
                    default="project.config")

# [必选] 执行的命令
# menuconfig  通过面板进行配置
# build       编译项目

cmd_help ='''
project command:

menuconfig: menuconfig panel
build:      start compile project, temp files in `build` dir, dist files in `dist` dir
rebuild:    update cmakefiles and build, if new file added, shoud use this command
clean:      clean build files, won't clean configuration
distclean:  clean all build files and configuration except configuration configed by config conmand
flash:      burn firmware to board's flash
'''

parser.add_argument("cmd",
                    help=cmd_help,
                    choices=["config", "build", "rebuild", "menuconfig", "clean", "distclean", "clean_conf", "flash"]
                    )


args = parser.parse_args()

if args.cmd == "menuconfig" :
    print('cmd=menuconfig')
    time_start = time.time()
    # 创建 build 目录
    if not os.path.exists("build"):
        os.mkdir("build")
    # color theme 颜色主题
    cmd_color_str = 'export MENUCONFIG_STYLE=aquatic'
    # 面板命令
    cmd_str = 'python ' + fs_sdk_path + 'tools/menuconfig.py ' + fs_sdk_path + 'Kconfig'
    print('cmd_str            ={}'.format(cmd_color_str + ' & ' + cmd_str))
    # 调用命令行 执行 menuconfig
    os.system(cmd_color_str + ' && ' + cmd_str)

